﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace Paltop_.Controllers
{
    public class AdminController
    {
      static  public  string AddNewExtention(string jsFunction, string title)
        {
            string retVal = "";
            Paltop_DatabaseLayer.PaltopDatabaseEntities db = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
            var setting = db.SYSMainSettings.Where(s => s.Title == "Extentions");
            XElement Extentions = XElement.Parse(setting.First().Setting);
            XElement Extention = new XElement("Extention");
            Extentions.Add(Extention);
            XElement tempElem = new XElement("Title");
            tempElem.Value = title.ToLower();
            Extention.Add(tempElem);
            tempElem = new XElement("JSFunction");
            tempElem.Value = jsFunction;
            Extention.Add(tempElem);
            setting.First().Setting = Extentions.ToString();
            var extentionTable = from E in db.FileExtentions
                                 where E.Title == title
                                 select E;
            if (extentionTable.Count() != 0)
            {
                extentionTable.First().JSFunction = jsFunction;
                retVal = "{\"FileExtentionID\":\"" + extentionTable.First().FileExtentionID + "\","
                    + "\"Title\":\"" + extentionTable.First().Title + "\","
                    + "\"JSFunction\":\"" + extentionTable.First().JSFunction + "\","
                     + "\"Available\":\"true\","
                    + "\"Description\":\"" + extentionTable.First().Description + "\"}";
                db.SaveChanges();
            }
            else
            {
                Paltop_DatabaseLayer.FileExtention newExtension = new Paltop_DatabaseLayer.FileExtention();
                newExtension.Title = title;
                newExtension.JSFunction = jsFunction;
                newExtension.Description = "";
                db.FileExtentions.AddObject(newExtension);

                db.SaveChanges();
                retVal = "{\"FileExtentionID\":\"" + newExtension.FileExtentionID + "\","
                   + "\"Title\":\"" + newExtension.Title + "\","
                   + "\"JSFunction\":\"" + newExtension.JSFunction + "\","
                    + "\"Available\":\"false\","
                   + "\"Description\":\"" + newExtension.Description + "\"}";
            }
            return retVal;
        }

      internal static string EditExtension(string jsfunction, string title, string decription, int eid)
      {
          Paltop_DatabaseLayer.PaltopDatabaseEntities db = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
          var extentionTable = from E in db.FileExtentions
                               where E.FileExtentionID==eid
                               select E;
          extentionTable.First().JSFunction = jsfunction;
          extentionTable.First().Title = title;
          extentionTable.First().Description = decription;
          db.SaveChanges();
          return "ok";
      }

      internal static string DisableExtension(int eid)
      {
          Paltop_DatabaseLayer.PaltopDatabaseEntities db = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
          var extentionTable = from E in db.FileExtentions
                               where E.FileExtentionID == eid
                               select E;
          extentionTable.First().JSFunction = "downloadUnknownType(this.id)";
          
          db.SaveChanges();
          return "ok";
      }
    }
}